//
//  NetworkRequest.m
//  cocopadsdownload
//
//  Created by ios on 16/9/8.
//  Copyright © 2016年 ios. All rights reserved.
//

#import "NetworkRequest.h"
#import "AFNetworking.h"

@implementation NetworkRequest
{
    AFHTTPSessionManager * _manager;
}


+ (id)shareInstance //获取网络请求单例
{
    static NetworkRequest *helper;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (helper == nil)
        {
            helper = [[NetworkRequest alloc] init];
        }
    });
    return helper;
}

-(instancetype)init{
    if (self ==[super init]) {
        
        _manager =[AFHTTPSessionManager manager];
        _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        //_manager.responseSerializer = [AFJSONResponseSerializer serializer];
        
        NSMutableSet *contentTypes = [[NSMutableSet alloc] initWithSet:_manager.responseSerializer.acceptableContentTypes];
        [contentTypes addObject:@"text/html"];//添加类型
        [contentTypes addObject:@"application/json"];
        [contentTypes addObject:@"charset=utf-8"];
        _manager.responseSerializer.acceptableContentTypes = contentTypes;
    }
    return self;
}

-(void)getRequest:(NSString *)url parameter:(NSDictionary *)parameter success:(success)successBlock failuer:(failuer)failuerBlock{
    
    [_manager GET:url parameters:parameter progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        return successBlock(dic);
       // return successBlock(responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        return failuerBlock(error);
        
    }];
    
}

-(void)postRequest:(NSString *)url parameter:(NSDictionary *)parameter success:(success)successBlock failuer:(failuer)failuerBlock{
    
   [_manager POST:url parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
       
   } progress:^(NSProgress * _Nonnull uploadProgress) {
       
   } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
       NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
       
       return successBlock(dic);
       
   } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
       
        return failuerBlock(error);
   }];
    
}

-(void)updataImage:(NSString *)url parameter:(NSDictionary *)parameter imageData:(NSData *)imageData fileName:(NSString *)fileName success:(success)successBlock failuer:(failuer)failuerBlock{
    
    [_manager POST:url parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
         [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/jpeg"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}



@end
